package com.cmsz.paydemo.function;

import java.io.IOException;

import com.cmsz.paydemo.util.FileUtil;
import com.cmsz.paydemo.util.HttpClientUtil;
import com.cmsz.paydemo.util.HttpClientUtil.NotificationProcessor;
import com.cmsz.paydemo.util.SignUtil;
import com.cmsz.paydemo.util.SimpleHttpServer;

/**
 * 
 * TODO 模拟商户支付的预下单和支付结果通知功能
 * 
 * @Date 2017年4月28日 下午2:20:04
 * @author Edison_lv
 * @version
 */
public class PayDemo {
	public static void main(String[] args) throws Exception {
		String requestData = FileUtil.getData("payReqData.xml");
	    requestData = SignUtil.sign(requestData);

		String responseData = HttpClientUtil.sendRequest("pay", requestData,
				new NotificationProcessor() {
					public void processNotification() throws IOException {
						//这里用另一个线程接受异步结果通知
				        new Thread(new Runnable() {  
				            public void run() { 
				            	try {
									Thread.sleep(1000);
									new SimpleHttpServer().service();
								} catch (InterruptedException e) {
									e.printStackTrace();  
								} catch (Exception e) {
									// TODO Auto-generated catch block  
									e.printStackTrace();  
								}
				            }  
				        }).start();
					}
				});
		System.out.println("同步返回报文："+responseData);
		//报文验签
		Boolean signFlag = SignUtil.checkSign(responseData);
		System.out.println("同步返回报文验签结果："+signFlag);
		// create form html and print
	}
}
